package com.eralink.unionuhfreader.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;

import com.eralink.unionuhfreader.MyApplication;
import com.eralink.unionuhfreader.R;
import com.eralink.unionuhfreader.base.BaseActivity;
import com.eralink.unionuhfreader.util.Canstant;
import com.eralink.unionuhfreader.util.SharedPreferencesUtil;

public class SettingActivity extends BaseActivity {
    private RadioGroup rdg_rfid,rdg_onecard;
    private RadioButton rdb_open,rdb_close,onecard_open,onecard_close;
    private Button btn_sure,btn_close;
    private EditText onecard_ip,onecard_port;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        rdg_rfid=findViewById(R.id.rdg_rfid);
        rdb_open=findViewById(R.id.rdb_open);
        rdb_close=findViewById(R.id.rdb_close);
        rdg_rfid.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==R.id.rdb_open){
                    SharedPreferencesUtil.saveData(SettingActivity.this, Canstant.RFID_SWITCH_KEY,1);
                }else if (i==R.id.rdb_close){
                    SharedPreferencesUtil.saveData(SettingActivity.this, Canstant.RFID_SWITCH_KEY,0);
                }
            }
        });
        /*一卡通*/
        rdg_onecard=findViewById(R.id.rdg_onecard);
        onecard_open=findViewById(R.id.onecard_open);
        onecard_close=findViewById(R.id.onecard_close);
        boolean b= (boolean) SharedPreferencesUtil.getData(SettingActivity.this, MyApplication.key_onecard,false);
        if (b){
            onecard_open.setChecked(true);
            onecard_close.setChecked(false);
        }else{
            onecard_open.setChecked(false);
            onecard_close.setChecked(true);
        }
        rdg_onecard.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==R.id.onecard_open){
                    SharedPreferencesUtil.saveData(SettingActivity.this, MyApplication.key_onecard,true);
                    MyApplication.connect_oncard=true;
                }else if (i==R.id.onecard_close){
                    SharedPreferencesUtil.saveData(SettingActivity.this, Canstant.RFID_SWITCH_KEY,false);
                    MyApplication.connect_oncard=false;
                }
            }
        });
        btn_sure=findViewById(R.id.btn_sure);
        btn_close=findViewById(R.id.btn_close);


        onecard_ip=findViewById(R.id.onecard_ip);
        onecard_port=findViewById(R.id.onecard_port);
        String cacherip= (String) SharedPreferencesUtil.getData(SettingActivity.this, MyApplication.key_onecard_ip,"");
        onecard_ip.setText(cacherip);
        String cacherport= (String) SharedPreferencesUtil.getData(SettingActivity.this, MyApplication.key_onecard_port,"");
        onecard_port.setText(cacherport);
        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ip=onecard_ip.getText().toString();
                String port=onecard_port.getText().toString();
                SharedPreferencesUtil.saveData(SettingActivity.this, MyApplication.key_onecard_ip,ip);
                SharedPreferencesUtil.saveData(SettingActivity.this,MyApplication.key_onecard_port,port);
                MyApplication.getInstance().NettyConnect();
                finish();
            }
        });
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        int selectindex= (int) SharedPreferencesUtil.getData(this,Canstant.RFID_SWITCH_KEY,0);
        if (selectindex==0){
            rdb_open.setChecked(false);
            rdb_close.setChecked(true);
        }else{
            rdb_open.setChecked(true);
            rdb_close.setChecked(false);
        }
    }
}
